<?php

namespace app\admin\service;

use app\common\model\Account;
use app\common\model\CashTransfer;
use app\common\model\ConsultingSubsidy;
use app\common\model\Trade;
use app\common\model\Transaction;
use ShidianUtil\Utils\TimeUtil;

/**
 * Class StatisticsService
 * @package app\admin\service
 */
class StatisticsService
{
    /**
     * 财务统计
     * @param array $params
     * @return array
     */
    public function read(array $params): array
    {

        $startTime = $params['startTime'];
        $endTime = $params['endTime'];
        if (is_numeric($endTime)) {
            $endTime = TimeUtil::stamp(TimeUtil::DAY, $endTime, false);
        }

        $query = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->consultingTrade();

        // 订单总额
        $order_money_sum = $query->sum('payMoney');
        // 订单总数
        $order_count = $query->count();

        // 平台补贴
        // $subsidy_sum = ConsultingSubsidy::timeRange($startTime, $endTime, 'created', 'created')->sum('subsidy');
        // 企业充值
        $transaction_sum = Transaction::timeRange($startTime, $endTime, 'created', 'created')->sum('amount');

        // 提现申请金额
        $cash_sum = CashTransfer::timeRange($startTime, $endTime, 'created', 'created')->sum('total');

        // 退款总额
        $refund_money_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('refundStatus', Trade::REFUNDED)
            ->consultingTrade()
            ->sum('payMoney');

        // 律师总余额
        $tableName = Account::getTable();
        $lawyer_account_sum = Account::leftJoin('member m', "$tableName.memberId=m.id and $tableName.payMethod='" . PAYMENT_ACCOUNT . "'")
            ->where('m.type', '1')
            ->sum('balance');

        // 案源消费
        $law_case_money_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_SOURCE)
            ->sum('payMoney');

        // 通信币消费
        $axb_money_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_AXB)
            ->sum('payMoney');

        $query = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('tradeType', TRADE_TOKEN_RECHARGE);

        // 案源充值笔数 + 通信币充值笔数
        $token_count = $query->count();

        // 案源充值金额 + 通信币充值金额
        $token_sum = $query->sum('payMoney');

        // 律师佣金结算金额
        $lawyer_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('settleLawyerId', '<>', '0')
            ->sum('settleLawyerMoney');

        // 余额支付充值费用总额
        $account_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
            ->where('payStatus', Trade::PAID)
            ->where('payMethod', PAYMENT_ACCOUNT)
            ->where('tradeType', TRADE_TOKEN_RECHARGE)
            ->sum('payMoney');

        return [
            ['dataIconImg' => image_url('/storage/static/images/caiwu/1.png'), 'name' => '订单总额', 'value' => money_show($order_money_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/2.png'), 'name' => '订单总数', 'value' => $order_count],
            // ['dataIconImg' => image_url('/storage/static/images/caiwu/3.png'), 'name' => '平台补贴', 'value' => money_show($subsidy_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/qiyechongzhi.png'), 'name' => '企业充值', 'value' => money_show($transaction_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/4.png'), 'name' => '提现申请金额', 'value' => money_show($cash_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/5.png'), 'name' => '退款总额', 'value' => money_show($refund_money_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/6.png'), 'name' => '律师总余额', 'value' => money_show($lawyer_account_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/7.png'), 'name' => '案源消费', 'value' => money_show($law_case_money_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/11.png'), 'name' => '通信币消费', 'value' => money_show($axb_money_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/12.png'), 'name' => '律师端充值笔数', 'value' => $token_count],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/13.png'), 'name' => '律师端充值金额', 'value' => money_show($token_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/16.png'), 'name' => '律师佣金', 'value' => money_show($lawyer_sum)],
            ['dataIconImg' => image_url('/storage/static/images/caiwu/17.png'), 'name' => '余额支付总金额', 'value' => money_show($account_sum)],
        ];
    }

}
